package com.leo.blog.web.bloger;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import com.leo.blog.login.SecurityUtils;
import com.leo.blog.login.SelfUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import com.leo.blog.dto.BlogerExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.enums.BlogerStateEnum;
import com.leo.blog.service.BlogerService;
import com.leo.blog.util.HttpServletRequestUtil;


@RestController
@RequestMapping("/bloger")
public class BlogerManagementController {

	@Autowired
	private BlogerService blogerService;
	
	@RequestMapping(value = "/getblogerlist")
	public Map<String, Object> getBlogerList(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		//获取前台传过来的值
		String blogerName = HttpServletRequestUtil.getString(request, "blogerName");
		int gender = HttpServletRequestUtil.getInt(request, "gender");
		int hasPermission = HttpServletRequestUtil.getInt(request, "hasPermission");
		
		Bloger blogerCondition = new Bloger();
		if(blogerName != null && blogerName != "") {
			blogerCondition.setBlogerName(blogerName);
		}
		if(gender > 0) {
			blogerCondition.setGender(gender);
		}
		if(hasPermission != 0) {
			blogerCondition.setHasPermission(hasPermission);
		}
		
		BlogerExecution be = blogerService.getBlogerList(blogerCondition, pageIndex, pageSize);
		if(be.getState() == BlogerStateEnum.SUCCESS.getState()) {
			//返回页数
			int page = 0;
			int pageCount = be.getCount();
			if(pageCount > pageSize) {
				if(pageCount%pageSize > 0) {
					page = pageCount/pageSize + 1;
				}else {
					page = pageCount/pageSize;
				}
				
			}else {
				page = 1;
			}
			
			modelMap.put("blogerList", be.getBlogerList());
			modelMap.put("count", be.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@GetMapping(value = "/getuserbyloginname")
	public Map<String, Object> getUserByLoginName(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		String userName = HttpServletRequestUtil.getString(request, "userName");
		String password = HttpServletRequestUtil.getString(request, "password");
		
		if(userName != null && userName != "") {

			BlogerExecution be = blogerService.getUserByLoginName(userName);
			if(be.getState() == BlogerStateEnum.SUCCESS.getState()) {
				modelMap.put("bloger", be.getBloger());
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "获取信息失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询失败,userName为空");
		}	
		return modelMap;
	}

	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;
	
	@PostMapping(value = "/updatebloger")
	public Map<String, Object> updateBloger(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogerId = HttpServletRequestUtil.getInt(request, "blogerId");
		String blogerName = HttpServletRequestUtil.getString(request, "blogerName");
		int gender = HttpServletRequestUtil.getInt(request, "gender");
		String blogerImage = HttpServletRequestUtil.getString(request, "blogerImage");
		String userName = HttpServletRequestUtil.getString(request, "userName");
		String password = HttpServletRequestUtil.getString(request, "password");
		password=bCryptPasswordEncoder.encode(password);
		//String passwordAndUserName = password + userName;
		//String passwordAndUserNameMD5 = DigestUtils.md5DigestAsHex(passwordAndUserName.getBytes());
		
		
		String email = HttpServletRequestUtil.getString(request, "email");
		int hasPermission = HttpServletRequestUtil.getInt(request, "hasPermission");
		
		Bloger bloger = new Bloger();
		if(blogerId > 0) {
			bloger.setBlogerId(blogerId);
		}
		if(blogerName != null && blogerName != "") {
			bloger.setBlogerName(blogerName);
		}
		if(gender != 0) {
			bloger.setGender(gender);
		}
		if(blogerImage != null && blogerImage != "") {
			bloger.setBlogerImage(blogerImage);
		}
		if(userName != null && userName != "") {
			bloger.setUserName(userName);
		}
		if(password != null && password != "") {
			bloger.setPassword(password);
		}
		if(email != null && email != "") {
			bloger.setEmail(email);
		}
		if(hasPermission != 0) {
			bloger.setHasPermission(hasPermission);
		}
		
		Bloger blogerSession = SecurityUtils.getBloger();
		if(blogerSession.getHasPermission() == 1) {
			BlogerExecution be = blogerService.updateBlogerSelective(bloger);
			if(be.getState() == BlogerStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}

		return modelMap;
	}
	
	@RequestMapping(value = "/addbloger")
	@ResponseBody
	public Map<String, Object> addBloger(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String blogerName = HttpServletRequestUtil.getString(request, "blogerName");
		int gender = HttpServletRequestUtil.getInt(request, "gender");
		String blogerImage = HttpServletRequestUtil.getString(request, "blogerImage");
		String userName = HttpServletRequestUtil.getString(request, "userName");
		//MD5加密+salt(盐)
		String password = HttpServletRequestUtil.getString(request, "password");//密码原值
		password=bCryptPasswordEncoder.encode(password);
		
		String email = HttpServletRequestUtil.getString(request, "email");
		int hasPermission = HttpServletRequestUtil.getInt(request, "hasPermission");
		
		Bloger bloger = new Bloger();
		if(blogerName != null && blogerName != "") {
			bloger.setBlogerName(blogerName);
		}
		if(gender != 0) {
			bloger.setGender(gender);
		}
		if(blogerImage != null && blogerImage != "") {
			bloger.setBlogerImage(blogerImage);
		}
		if(userName != null && userName != "") {
			bloger.setUserName(userName);
		}
		if(password != null && password != "") {
			bloger.setPassword(password);
		}
		if(email != null && email != "") {
			bloger.setEmail(email);
		}
		if(hasPermission != 0) {
			bloger.setHasPermission(hasPermission);
		}
		
		/**
		 * session
		 */	
		Bloger blogerSession = (Bloger)request.getSession().getAttribute("bloger");
		
		if(blogerSession.getHasPermission() == 1) {
			BlogerExecution be = blogerService.addBloger(bloger);
			if(be.getState() == BlogerStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "添加数据失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deleteblogerbyid")
	@ResponseBody
	public Map<String, Object> deleteBlogerById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogerId = HttpServletRequestUtil.getInt(request, "blogerId");
		
		//session
		Bloger bloger = SecurityUtils.getBloger();
		if(bloger.getHasPermission() == 1) {
			BlogerExecution be = blogerService.deleteBlogerById(blogerId);
			if(be.getState() == BlogerStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@PostMapping(value = "/getblogerbyid")
	public Map<String, Object> getBlogerById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int blogerId = HttpServletRequestUtil.getInt(request, "blogerId");
		
		try {
			BlogerExecution be = blogerService.getBlogerById(blogerId);
			modelMap.put("success", true);
			modelMap.put("bloger", be.getBloger());
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", "查询信息失败");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getblogerbyuserid")
	@ResponseBody
	public Map<String, Object> getBlogerByUserId(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//session
		SelfUserEntity selfUserEntity= SecurityUtils.getSysUser();
		try {
			BlogerExecution be = blogerService.getBlogerById(Integer.parseInt(selfUserEntity.getUserId()));
			modelMap.put("success", true);
			modelMap.put("bloger", be.getBloger());
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", "查询信息失败");
		}
		return modelMap;
	}
}

